package com.mxpio.erp.oa.controller;

import com.mxpio.erp.common.enums.BizExceptionEnums;
import com.mxpio.erp.common.enums.OrderStatusEnums;
import com.mxpio.erp.common.exception.BizException;
import com.mxpio.erp.common.model.request.BpmnSubmitRequest;
import com.mxpio.erp.common.oa.entity.HireApply;
import com.mxpio.erp.common.oa.service.HireApplyService;
import com.mxpio.erp.common.util.AssertUtil;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "HireApplyController", description = "招聘申请表")
@RestController
@RequestMapping("/erp/oa/hire/")
public class HireApplyController {

    @Resource
    private HireApplyService hireApplyService;

    @GetMapping("list")
    @Operation(summary = "招聘申请表列表", description = "招聘申请表列表（无分页）", method = "GET")
    public Result<List<HireApply>> list(@Parameter(description = "查询参数") Criteria criteria) throws UnsupportedEncodingException {
        try {
            List<HireApply> list = hireApplyService.list(HireApply.class, criteria);
            return Result.OK(list);
        } catch (BizException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error(BizExceptionEnums.SERVER_ERROR.getMsg());
        }
    }

    @GetMapping("page")
    @Operation(summary = "招聘申请表列表", description = "招聘申请表列表（分页）", method = "GET")
    public Result<Page<HireApply>> page(@Parameter(description = "查询参数") Criteria criteria, @Parameter(description = "每页条数") Integer pageSize, @Parameter(description = "页号") Integer pageNo) throws UnsupportedEncodingException {
        try {
            Pageable page = PageRequest.of(pageNo - 1, pageSize);
            Page<HireApply> pageResult = hireApplyService.listPage(HireApply.class, page, criteria);
            return Result.OK(pageResult);
        } catch (BizException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error(BizExceptionEnums.SERVER_ERROR.getMsg());
        }
    }

    @GetMapping("list/{bizNo}")
    @Operation(summary = "根据ID获取招聘申请表", description = "根据ID获取招聘申请表", method = "GET")
    public Result<HireApply> getById(@PathVariable(name = "bizNo", required = true) String bizNo) {
        try {
            HireApply hireApply = hireApplyService.getById(HireApply.class, bizNo);
            return Result.OK(hireApply);
        } catch (BizException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error(BizExceptionEnums.SERVER_ERROR.getMsg());
        }
    }

    @RequestMapping("add")
    @Operation(summary = "保存招聘申请表", description = "保存招聘申请表")
    public Result<HireApply> add(@RequestBody HireApply hireApply) {
        try {
            return Result.OK(hireApplyService.doSave(hireApply));
        } catch (BizException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error(BizExceptionEnums.SERVER_ERROR.getMsg());
        }
    }

    @PutMapping("edit")
    @Operation(summary = "编辑", description = "编辑", method = "PUT")
    public Result<HireApply> edit(@RequestBody HireApply hireApply) {
        try {
            AssertUtil.notNull(hireApply, BizExceptionEnums.PARAM_NOT_NULL);
            AssertUtil.notEmpty(hireApply.getBizNo(), BizExceptionEnums.PARAM_NOT_EMPTY, "bizNo");
            HireApply dbData = hireApplyService.getById(HireApply.class, hireApply.getBizNo());
            AssertUtil.notNull(dbData, BizExceptionEnums.ORDER_NOT_FOUND, hireApply.getBizNo());

            if (!dbData.getBpmnStatus().equals(OrderStatusEnums.BpmnStatus.CREATE.getStatusCode())) {
                throw new BizException(BizExceptionEnums.ORDER_UNABLE_UPDATE_BY_STATUS, OrderStatusEnums.BpmnStatus.CREATE.getStatusCode());
            }

            hireApplyService.update(hireApply);
            return Result.OK(hireApply);
        } catch (BizException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error(BizExceptionEnums.SERVER_ERROR.getMsg());
        }
    }

    @DeleteMapping("remove/{ids}")
    @Operation(summary = "根据id删除单子", description = "删除单子,多个id用英文逗号分隔", method = "DELETE")
    public Result<HireApply> remove(@PathVariable(name = "ids", required = true) String ids) {
        try {
            String[] id = ids.split(",");
            for (String key : id) {
                HireApply dbHireApply = hireApplyService.getById(HireApply.class, key);
                AssertUtil.notNull(dbHireApply, BizExceptionEnums.ORDER_NOT_FOUND);
                if (!dbHireApply.getBpmnStatus().equals(OrderStatusEnums.BpmnStatus.CREATE.getStatusCode())) {
                    throw new BizException(BizExceptionEnums.ORDER_UNABLE_DELETE_BY_STATUS, OrderStatusEnums.BpmnStatus.CREATE.getStatusCode());
                }
                hireApplyService.delete(HireApply.class, key);
            }
            return Result.OK();
        } catch (BizException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error(BizExceptionEnums.SERVER_ERROR.getMsg());
        }
    }

    @RequestMapping("submit")
    @Operation(summary = "提交申请-工作流", description = "提交申请-工作流")
    public Result<HireApply> submit(@RequestBody BpmnSubmitRequest request) {
        try {
            return Result.OK(hireApplyService.submit(request));
        } catch (BizException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error(BizExceptionEnums.SERVER_ERROR.getMsg());
        }
    }

    @RequestMapping("submit/Ok")
    @Operation(summary = "提交申请-工作流审核", description = "提交申请-工作流审核")
    public Result<HireApply> submitOK(@RequestBody BpmnSubmitRequest request) {
        try {
            return Result.OK(hireApplyService.audit(request));
        } catch (BizException e) {
            return Result.error(e.getMessage());
        } catch (Exception e) {
            return Result.error(BizExceptionEnums.SERVER_ERROR.getMsg());
        }
    }

}